<?php
namespace app\api\middleware;

use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Response;
use Throwable;

class ExceptionToApi extends Handle
{
    public function render($request, Throwable $e): Response
    {
        // 参数验证错误
        if ($e instanceof ValidateException) {
            return json(['code'=>($e->getCode()==0?422:$e->getCode()),'message'=>$e->getError()]);
        }

//        var_dump($e->getCode());
        return json(['code'=>($e->getCode()==0?405:$e->getCode()),'message'=>$e->getMessage()]);
    }

}